package com.tbit.main.controller;

import cn.hutool.core.collection.CollUtil;
import com.tbit.main.common.JsonResult;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.constant.WorkOrderConstant;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.AccountUserWorkConfig;
import com.tbit.main.service.AccountUserWorkConfigService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 运维人员工单配置
 *
 * @author LMY
 * @create 2021-08-25 17:39
 */
@RestController
@RequestMapping("/accountUserWorkConfigController")
public class AccountUserWorkConfigController {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private AccountUserWorkConfigService accountUserWorkConfigService;


    /**
     * 运维人员工单配置添加修改
     *
     * @param token
     * @param workType
     * @param workNumber
     * @return
     */
    @RequestMapping("/add")
    @OperateLog(module = ModuleEnum.ACCOUNTUSERWORKCONFIG, methods = MethodEnum.ACCOUNT_USER_WORK_CONFIG_ADD)
    public JsonResult add(String token, Integer accountId, Integer accountUserId,
                          @RequestParam(value = "workType") List<Integer> workType,
                          @RequestParam(value = "workNumber") List<Integer> workNumber,
                          @RequestParam(value = "locationTypes") List<Integer> locationTypes) {
        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        if (CollUtil.isEmpty(workType)) {
            throw new BaseException("WorkType.is.notNull");
        }
        if (CollUtil.isEmpty(workNumber)) {
            throw new BaseException("Classes.is.notNull");
        }

        /*
         * 删除
         */
        accountUserWorkConfigService.del(accountUserId);

        List<AccountUserWorkConfig> accountUserWorkConfigList = new LinkedList<>();
        for (Integer value : workType) {
            for (Integer integer : workNumber) {
                AccountUserWorkConfig accountUserWorkConfig = new AccountUserWorkConfig();
                accountUserWorkConfig.setAccountId(accountId);
                accountUserWorkConfig.setWorkType(value);
                accountUserWorkConfig.setWorkNumber(integer);
                accountUserWorkConfig.setAccountUserId(accountUserId);
                accountUserWorkConfigList.add(accountUserWorkConfig);
            }
        }

        for (Integer locationType : locationTypes) {
            for (Integer integer : workNumber) {
                AccountUserWorkConfig accountUserWorkConfig = new AccountUserWorkConfig();
                accountUserWorkConfig.setAccountId(accountId);
                accountUserWorkConfig.setWorkType(WorkOrderConstant.workType_NC);
                accountUserWorkConfig.setWorkNumber(integer);
                accountUserWorkConfig.setAccountUserId(accountUserId);
                accountUserWorkConfig.setLocationType(locationType);
                accountUserWorkConfigList.add(accountUserWorkConfig);
            }
        }

        accountUserWorkConfigService.addList(accountUserWorkConfigList);

        return JsonResult.succ();
    }

    /**
     * 删除人员
     *
     * @param token
     * @param accountUserId
     * @return
     */
    @RequestMapping("/del")
    @OperateLog(module = ModuleEnum.ACCOUNTUSERWORKCONFIG, methods = MethodEnum.ACCOUNT_USER_WORK_CONFIG_DEL)
    public JsonResult del(String token, Integer accountUserId) {

        Assert.notNull(accountUserId, "AccountUserId.notNull");
        accountUserWorkConfigService.del(accountUserId);
        return JsonResult.succ();
    }

    /**
     * 查询运营区域人员
     *
     * @param token
     * @param accountId
     * @return
     */
    @RequestMapping("/getAccountId")
    public JsonResult getAccountId(String token, Integer accountId,
                                   @RequestParam(value = "regionId", required = false) List<Integer> regionId) {

        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        AccountUser accountUser = tokenService.getAccount(token);

        List<AccountUserWorkConfig> accountUserWorkConfigList = accountUserWorkConfigService.getAccountId(accountId, regionId, accountUser.getAccountUserId());

        Map<Integer, AccountUserWorkConfig> accountUserWorkConfigMap = new HashMap<>();
        for (AccountUserWorkConfig accountUserWorkConfig : accountUserWorkConfigList) {
            if (accountUserWorkConfigMap.containsKey(accountUserWorkConfig.getAccountUserId())) {
                accountUserWorkConfigMap.get(accountUserWorkConfig.getAccountUserId()).getWorkTypes().add(accountUserWorkConfig.getWorkType());
                accountUserWorkConfigMap.get(accountUserWorkConfig.getAccountUserId()).getWorkNumbers().add(accountUserWorkConfig.getWorkNumber());
                accountUserWorkConfigMap.get(accountUserWorkConfig.getAccountUserId()).addLocationType(accountUserWorkConfig.getLocationType());
            } else {
                AccountUserWorkConfig accountUserConfig = new AccountUserWorkConfig();
                accountUserConfig.setAccountUserId(accountUserWorkConfig.getAccountUserId());
                accountUserConfig.setAccountId(accountUserWorkConfig.getAccountId());
                Set<Integer> workTypeSet = new HashSet<>();
                workTypeSet.add(accountUserWorkConfig.getWorkType());
                accountUserConfig.setWorkTypes(workTypeSet);

                Set<Integer> workNumberSet = new HashSet<>();
                workNumberSet.add(accountUserWorkConfig.getWorkNumber());
                accountUserConfig.setWorkNumbers(workNumberSet);

                accountUserConfig.addLocationType(accountUserWorkConfig.getLocationType());

                accountUserWorkConfigMap.put(accountUserWorkConfig.getAccountUserId(), accountUserConfig);
            }
        }

        Collection<AccountUserWorkConfig> values = accountUserWorkConfigMap.values();

        return JsonResult.succ(values);
    }

    /**
     * 根据运维人员查询
     *
     * @param token
     * @param accountUserId
     * @return
     */
    @RequestMapping("/getAccountUserId")
    public JsonResult getAccountUserId(String token, Integer accountUserId) {

        Assert.notNull(accountUserId, "AccountUserId.notNull");

        List<AccountUserWorkConfig> accountUserWorkConfigList = accountUserWorkConfigService.getAccountUserId(accountUserId);

        Map<Integer, AccountUserWorkConfig> accountUserWorkConfigMap = new HashMap<>();
        for (AccountUserWorkConfig accountUserWorkConfig : accountUserWorkConfigList) {
            if (accountUserWorkConfigMap.containsKey(accountUserWorkConfig.getAccountUserId())) {
                accountUserWorkConfigMap.get(accountUserWorkConfig.getAccountUserId()).getWorkTypes().add(accountUserWorkConfig.getWorkType());
                accountUserWorkConfigMap.get(accountUserWorkConfig.getAccountUserId()).getWorkNumbers().add(accountUserWorkConfig.getWorkNumber());
                accountUserWorkConfigMap.get(accountUserWorkConfig.getAccountUserId()).addLocationType(accountUserWorkConfig.getLocationType());
            } else {
                AccountUserWorkConfig accountUserConfig = new AccountUserWorkConfig();
                accountUserConfig.setAccountUserId(accountUserWorkConfig.getAccountUserId());
                accountUserConfig.setAccountId(accountUserWorkConfig.getAccountId());
                Set<Integer> workTypeSet = new HashSet<>();
                workTypeSet.add(accountUserWorkConfig.getWorkType());
                accountUserConfig.setWorkTypes(workTypeSet);

                Set<Integer> workNumberSet = new HashSet<>();
                workNumberSet.add(accountUserWorkConfig.getWorkNumber());
                accountUserConfig.setWorkNumbers(workNumberSet);

                accountUserConfig.addLocationType(accountUserWorkConfig.getLocationType());

                accountUserWorkConfigMap.put(accountUserWorkConfig.getAccountUserId(), accountUserConfig);
            }
        }

        Collection values = accountUserWorkConfigMap.values();
        return JsonResult.succ(values);
    }

}
